define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'doublebox-bootstrap'], function ($, undefined, Backend, Table, Form, Doublebox) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index?class_id=' + Config.user.class_id,
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        //{field: 'group.name', title: __('Group')},
                        {field: 'username', visible: Config.username, title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', visible: Config.nickname, title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', visible: Config.mobile, title: __('Mobile'), operate: 'LIKE'},
                        {field: 'school_class.class_name', visible: Config.class, title: '班级', operate: false},

                        {field: 'money', visible: Config.money, title: '余额', operate: 'LIKE'},
                        {field: 'discount_amount', visible: Config.discount_amount, title: '优惠课时', operate: 'LIKE'},
                        {field: 'csmmeet', visible: Config.discount_amount, title: '课室租用（次）', operate: 'LIKE'},
                        {
                            field: 'gender',
                            title: __('Gender'),
                            visible: Config.gender,
                            searchList: {"1": __('Male'), "0": __('Female')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'status',
                            visible: Config.status,
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {normal: __('Normal'), hidden: __('Hidden')}
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        },
                        {
                            field: 'buttons',
                            width: "350px",
                            align: 'left',
                            title: '其他操作',
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '详情',
                                    visible: Config.info,
                                    title: function (row) {
                                        return row.nickname;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'user/user/info',
                                },
                                {
                                    name: 'detail',
                                    text: '充值缴费',
                                    visible: Config.recharge,
                                    extend: "data-area=\"['300px', '200px']\"",
                                    title: function (row) {
                                        return row.nickname;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'user/user/recharge',
                                },
                                {
                                    name: 'detail',
                                    text: '请假',
                                    visible: Config.leave,
                                    title: function (row) {
                                        return row.nickname;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'user/user/leave',
                                },
                                {
                                    name: 'detail',
                                    text: '解绑',
                                    visible: function (row) {
                                        //console.log(Config.unboundx);
                                        if (!Config.unboundx) {
                                            return Config.unboundx;
                                        }
                                        // console.log(row.school_class.is_weixin);
                                        return row.school_class.is_weixin;
                                    },
                                    title: function (row) {
                                        return row.nickname;
                                    },
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'user/user/unbound',
                                    confirm: '确认解绑？',
                                    success: function (data, ret) {
                                        Fast.api.success('解绑成功');
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        //  console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '购买优惠课时',
                                    visible: Config.purchase,
                                    extend: "data-area=\"['300px', '200px']\"",
                                    title: function (row) {
                                        return row.nickname;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'user/user/purchase',
                                },
                                {
                                    name: 'detail',
                                    text: '购买课室次数',
                                    visible: Config.purchase,
                                    extend: "data-area=\"['300px', '200px']\"",
                                    title: function (row) {
                                        return row.nickname;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'user/user/csmmeet',
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });
            $(document).on('click', '.btn-class_add', function () {
                Fast.api.open('user/user/class_add?class_id=' + Config.user.class_id, '添加', []);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        info: function () {
// 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/info/user_id/' + Config.user_id + location.search,

                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'SchoolClass.class_name', title: '班级名称'},
                        {
                            field: 'use_class_hour',
                            title: '上课进度',
                            formatter: function (row, data) {
                                // console.log(data);
                                var jindu = (data.use_class_hour / data.total_class_hour * 100);
                                return '<div class="progress" style="background-color:#a9a9a9a9">\n' +
                                    '        <div class="progress-bar" style="width:' + jindu + '%;" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" >' + data.use_class_hour + '/' + data.total_class_hour + '</div>\n' +
                                    '    </div>';
                            }
                        },
                        {
                            field: 'use_class_hour',
                            title: '班级进度',
                            formatter: function (row, data) {
                                //console.log(data);
                                var jindu = (data.SchoolClass.use_class_hour / data.SchoolClass.total_class_hour * 100);
                                return '<div class="progress" style="background-color:#a9a9a9a9">\n' +
                                    '        <div class="progress-bar" style="width:' + jindu + '%;" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" >' + data.SchoolClass.use_class_hour + '/' + data.SchoolClass.total_class_hour + '</div>\n' +
                                    '    </div>';
                            }
                        },
                       /* {
                            field: 'status',
                            title: '学生状态',
                            searchList: {"0": '学习中', "1": '毕业', "2": '退班'},
                            formatter: Table.api.formatter.status
                        },
                        //{field: 'total_class_hour', title: __('Total_class_hour')},
                        //{field: 'use_class_hour', title: '已用课时'},
                        /* {
                             field: 'createtime',
                             title: __('Createtime'),
                             operate: 'RANGE',
                             addclass: 'datetimerange',
                             formatter: Table.api.formatter.datetime
                         },*/
                    ]
                ]
            });
          /**  layui.use('element', function () {
                var element = layui.element;
            });*/
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        leave: function () {
            Controller.api.bindevent();
        },
        recharge: function () {
            Controller.api.bindevent();
        },
        purchase: function () {
            Controller.api.bindevent();
        },
        csmmeet: function () {
            Controller.api.bindevent();
        },
        class_add: function () {
            Controller.api.bindevent();
            $(document).ready(function () {
                var demo2 = $('.demo').doublebox({
                    nonSelectedListLabel: '所有学生',
                    selectedListLabel: '班级学生',
                    preserveSelectionOnMove: 'all',
                    moveOnSelect: true,
                    nonSelectedList: Config.classlist.class_2,
                    selectedList: Config.classlist.class_1,
                    optionValue: "id",
                    optionText: "nickname",
                    doubleMove: true,
                });
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});